package com.jt.controller;

import com.jt.pojo.User;
import com.jt.service.UserService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @outhor fufeixiang
 * @date 2021-05-06
 */
@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<User> userList = userService.findAll();
        model.addAttribute("userList", userList);
        return "userList";
    }
    /*
     * 需求1: 利用restFul实现用户数据修改
     *  之后重定向到userList.html页面
     *  URL地址: /user/id/name
     * */

    @RequestMapping("/updateUser/{id}/{name}")
    public String updateUser(@PathVariable Integer id,@PathVariable String name){
        userService.updateUser(id,name);
        return "redirect:/findAll";
    }
    /*
    * 需求2：
    *       利用restFul实现数据删除
    *       url：/user/100
    *       之后重定向到userList.html页面
    * */
    @RequestMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable Integer id){
        userService.deleteUser(id);
        return "redirect:/findAll";
    }

    /*
    * 要求跳转到userAjax页面中
    *
    * */
    @RequestMapping("/userAjax")
    public String userAjax(){

        return "userAjax";
    }
    /**/
    @RequestMapping("/findAjaxUser")
    @ResponseBody
    public List<User> findAjaxUser(){

        return userService.findAll();
    }
    @RequestMapping("/updateAjaxUser")
    @ResponseBody
    public void updateAjaxUser(User user){

        userService.updateUser(user);
    }

}
